package io.renren.modules.noc.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.noc.entity.NocBeerTicketBusinessEntity;
import io.renren.modules.noc.service.NocBeerTicketBusinessService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.noc.entity.NocBeerTicketMemberEntity;
import io.renren.modules.noc.service.NocBeerTicketMemberService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 啤酒节-门票-观众
 *
 * @author whc
 * @email hucheng_86@126.com
 * @date 2019-07-05 16:04:47
 */
@RestController
@RequestMapping("noc/nocbeerticketmember")
public class NocBeerTicketMemberController {
    @Autowired
    private NocBeerTicketMemberService nocBeerTicketMemberService;
    @Autowired
    private NocBeerTicketBusinessService nocBeerTicketBusinessService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("noc:nocbeerticketmember:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = nocBeerTicketMemberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("noc:nocbeerticketmember:info")
    public R info(@PathVariable("id") Integer id){
        NocBeerTicketMemberEntity nocBeerTicketMember = nocBeerTicketMemberService.getById(id);

        return R.ok().put("nocBeerTicketMember", nocBeerTicketMember);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("noc:nocbeerticketmember:save")
    public R save(@RequestBody NocBeerTicketMemberEntity nocBeerTicketMember){
        nocBeerTicketMemberService.save(nocBeerTicketMember);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("noc:nocbeerticketmember:update")
    public R update(@RequestBody NocBeerTicketMemberEntity nocBeerTicketMember){
        ValidatorUtils.validateEntity(nocBeerTicketMember);
        nocBeerTicketMemberService.updateById(nocBeerTicketMember);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("noc:nocbeerticketmember:delete")
    public R delete(@RequestBody Integer[] ids){
        nocBeerTicketMemberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/in")
    public R  in(@RequestParam Map<String, Object> params){
        String id = (String)params.get("id");
        String type = (String)params.get("type");
        Map<String,Object> map = new HashMap<>();
        map.put("tid",id);
        map.put("type",type);
        //记录进场日志
        nocBeerTicketMemberService.in(map);
        //签到
        if("1".equals(type)) {
            NocBeerTicketMemberEntity ticket = new NocBeerTicketMemberEntity();
            ticket.setId(Integer.parseInt(id));
            ticket.setIsSign("1");
            ticket.setSignTime(new Date());
            nocBeerTicketMemberService.updateById(ticket);
        }else if("2".equals(type)){
            NocBeerTicketBusinessEntity ticket = new NocBeerTicketBusinessEntity();
            ticket.setId(Integer.parseInt(id));
            ticket.setIsSign("1");
            ticket.setSignTime(new Date());
            nocBeerTicketBusinessService.updateById(ticket);
        }
        //获取统计数据
        String TotalMember = nocBeerTicketMemberService.getTotalMember(map);
        String TotalCount = nocBeerTicketMemberService.getTotalCount(map);
        Map<String,Object> lastMap = new HashMap<>();
        lastMap.put("intime",new Date());
        lastMap.put("TotalMember",TotalMember);
        lastMap.put("TotalCount",TotalCount);
        return R.ok(lastMap);
    }

    @RequestMapping("/out")
    public R  out(@RequestParam Map<String, Object> params){
        Integer tid = Integer.valueOf((String)params.get("id"));

        Map<String,Object> map = new HashMap<>();
        map.put("tid",tid);
        nocBeerTicketMemberService.out(map);
        Date date1 =nocBeerTicketMemberService.getInTime(map);
        Date date2 = nocBeerTicketMemberService.getOutTime(map);
        String TotalMember = nocBeerTicketMemberService.getTotalMember(map);
        String TotalCount = nocBeerTicketMemberService.getTotalCount(map);
        Map<String,Object> lastMap = new HashMap<>();
        lastMap.put("intime",date1);
        lastMap.put("outtime",date2);
        lastMap.put("TotalMember",TotalMember);
        lastMap.put("TotalCount",TotalCount);
        return R.ok(lastMap);
    }


}
